import React, {Component} from 'react'
import F from 'lodash/fp'

const PageItem = props => {
    const {page, onSwitch, cur} = props
    return (
        <li className={page === cur ? 'am-active' : null}>
            <a href="javascript:void(0)" onClick={() => onSwitch(page)}>{page + 1}</a>
        </li>
    )
}

class Pagination extends Component {
    constructor(props) {
        super(props)
        this.state = {page: 0}
    }

    handleSwitch(page) {
        let self = this
        self.setState({page})
        self.props.onSwitch(page)
    }

    render() {
        const {count, limit, onSwitch, page} = this.props
        const pageCount = Math.ceil(count/limit)
        return (
            <ul className="am-pagination">
                {F.times(i => <PageItem page={i} cur={this.state.page} key={i} onSwitch={this.handleSwitch.bind(this)} />, pageCount)}
            </ul>
        )
    }
}

Pagination.defaultProps = {
    count: 0,
    limit: 20,
    onSwitch: () => null
}

export default Pagination